#include "stdio.h"              //C51: rom-2736  ram-106
#include "windows.h"
#include "winuser.h"

#include "ButtonManager.h"

static uint8_t Button1Read(void);
static uint8_t Button2Read(void);
static uint8_t Button3Read(void);

static void Button1EventCallback(uint8_t event);
static void Button2EventCallback(uint8_t event);
static void Button3EventCallback(uint8_t event);

static void Button12GroupEventCallback(void);
static void Button23GroupEventCallback(void);

static void WINAPI timerCallback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);

static const char *keyEventString[] = {
    "ButtonEventNone",
	"ButtonEventPressTrig",	
	"ButtonEventReleaseTrig",	 
	"ButtonEventClicked",		 
	"ButtonEventDClicked",		 
	"ButtonEventLongPressed",	 
	"ButtonEventLongPressRepeat",
};

// 按键1
static ButtonNode button1;

// 按键2
static ButtonNode button2;

// 按键3
static ButtonNode button3;

// 组合键1 & 2
static ButtonGroupNode button12Group;

// 组合键2 & 3
static ButtonGroupNode button23Group;

int main(int argc, char *argv[])
{
    ButtonInit(&button1.button, Button1Read);
    ButtonInit(&button2.button, Button2Read);
    ButtonInit(&button3.button, Button3Read);       // 初始化按键对象

    ButtonManagerAddButton(&button1, Button1EventCallback);     // 添加按键到管理器
    ButtonManagerAddButton(&button2, Button2EventCallback);
    ButtonManagerAddButton(&button3, Button3EventCallback);

    ButtonManagerAddButtonGroup(&button12Group, 3000, Button12GroupEventCallback);    // 添加组合键到管理器(长按3S触发)
    ButtonManagerGroupSetItem(&button12Group, 0, 0, ButtonGroudActivePressed);  // 设置组合键键0为按键0，按下有效
    ButtonManagerGroupSetItem(&button12Group, 1, 1, ButtonGroudActivePressed);  // 设置组合键键1为按键1，按下有效

    ButtonManagerAddButtonGroup(&button23Group, 5000, Button23GroupEventCallback);    // 添加组合键到管理器(长按5S触发)
    ButtonManagerGroupSetItem(&button23Group, 0, 1, ButtonGroudActivePressed);  // 设置组合键键0为按键1，按下有效
    ButtonManagerGroupSetItem(&button23Group, 1, 2, ButtonGroudActivePressed);  // 设置组合键键1为按键2，按下有效

    int timerID = timeSetEvent(ButtonTick, 1, (LPTIMECALLBACK)timerCallback, 0, TIME_PERIODIC);

    while(1)
    {
        Sleep(1);
    }

    timeKillEvent(timerID);    //关闭定时器
}

/**
 * @brief 时基定时器中断
*/
static void WINAPI timerCallback(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
    ButtonManagerHandler();     // 按键管理器事务处理
}

/**
 * @brief 按键1事件处理
*/
static void Button1EventCallback(uint8_t event)
{
    printf("button1 event: %s\n", keyEventString[event]);
}

/**
 * @brief 按键2事件处理
*/
static void Button2EventCallback(uint8_t event)
{
    printf("button2 event: %s\n", keyEventString[event]);
    if(event == ButtonEventClicked)
        ButtonManagerDisableGroup(0);
}

/**
 * @brief 按键3事件处理
*/
static void Button3EventCallback(uint8_t event)
{
    printf("button3 event: %s\n", keyEventString[event]);
    if(event == ButtonEventClicked)
        ButtonManagerEnableGroup(0);
}

/**
 * @brief 按键1和2组合按下事件处理
*/
static void Button12GroupEventCallback(void)
{
    printf("button 1 & 2 pressed\n");
    ButtonManagerDisableButton(0);
}

/**
 * @brief 按键2和3组合按下事件处理
*/
static void Button23GroupEventCallback(void)
{
    printf("button 2 & 3 pressed\n");
    ButtonManagerEnableButton(0);
}

/**
 * @brief 按键1状态读取
*/
static uint8_t Button1Read(void)
{
    return GetKeyState(0x41) < 0;   // 'a'
}

/**
 * @brief 按键2状态读取
*/
static uint8_t Button2Read(void)
{
    return GetKeyState(0x42) < 0;   // 'b'
}

/**
 * @brief 按键3状态读取
*/
static uint8_t Button3Read(void)
{
    return GetKeyState(0x43) < 0;   // 'c'
}